package cn.tedu.store.product.webapi.service.impl;

import cn.tedu.store.commons.pojo.product.entity.CategoryAttributeTemplate;
import cn.tedu.store.commons.pojo.product.vo.AttributeTemplateStandardVO;
import cn.tedu.store.commons.pojo.product.vo.CategoryAttributeTemplateStandardVO;
import cn.tedu.store.commons.pojo.product.vo.CategoryStandardVO;
import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.product.webapi.mapper.AttributeTemplateMapper;
import cn.tedu.store.product.webapi.mapper.CategoryAttributeTemplateMapper;
import cn.tedu.store.product.webapi.mapper.CategoryMapper;
import cn.tedu.store.product.service.ICategoryAttributeTemplateService;
import cn.tedu.store.commons.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 分类和属性模板关系业务实现类
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Slf4j
@Service
public class CategoryAttributeTemplateServiceImpl implements ICategoryAttributeTemplateService {

    @Autowired
    private CategoryAttributeTemplateMapper categoryAttributeTemplateMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AttributeTemplateMapper attributeTemplateMapper;

    @Override
    public void bind(Long categoryId, Long attributeTemplateId) {
        log.debug("开始处理[绑定分类和属性模板]业务 参数:分类id={},属性模板id={}", categoryId, attributeTemplateId);
        //检查分类是否存在
        CategoryStandardVO categoryResult = categoryMapper.getStandardById(categoryId);
        if(categoryResult == null){
            String message = "绑定失败,分类不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //检查属性模板是否存在
        AttributeTemplateStandardVO attributeTemplateResult = attributeTemplateMapper.getStandardById(attributeTemplateId);
        if(attributeTemplateResult == null){
            String message = "绑定失败,属性模板不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //检查是否已经存在绑定关系
        int count = categoryAttributeTemplateMapper.countByCategoryAndAttributeTemplate(categoryId, attributeTemplateId);
        if(count != 0){
            String message = "绑定失败,此分类于此属性模板的绑定关系已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //执行绑定
        CategoryAttributeTemplate categoryAttributeTemplate = new CategoryAttributeTemplate();
        categoryAttributeTemplate.setAttributeTemplateId(attributeTemplateId);
        categoryAttributeTemplate.setCategoryId(categoryId);
        int rows = categoryAttributeTemplateMapper.insert(categoryAttributeTemplate);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void unbind(Long id) {
        log.debug("开始处理[解绑分类和属性模板]业务 参数:{}",id);
        CategoryAttributeTemplateStandardVO queryResult = categoryAttributeTemplateMapper.getStandardById(id);
        if(queryResult == null){
            String message = "解除绑定失败,此关系不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        //执行解绑
        int rows = categoryAttributeTemplateMapper.deleteById(id);
        if(rows != 1){
            String message = "操作失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }
}

